<?php

class UserPoint {
	
	/** @TplVar("USER_POINT_FIELD") */
	private static $field_name = "Points";
	private static $table_name = "tcnvmod_userpoint";


	/** 
	 * @Datasource
	 * @var TcnvSQL 
	 */
	private static $datasource;


	/** @InterceptPostRow */
	public function postRow($action,$postrow){
		
		$postrow['POSTER_POINT'] = $this->getPoint($postrow['POSTER_ID']);
	}
	
	private function getPoint($user_id) {
		/* @var $sql TcnvSQL */
		$sql = self::$datasource;
		
		$result_query = $sql->query("SELECT point FROM zs_user_point WHERE user=".$user_id);
		$result = $sql->feach_array($result_query);
		
		$point = $result[0];
		
		if(!isset ($point) || $point == "") $point = 0;
		return $point;
	}
	
	/**
	 * Manage the point inrementation on post
	 * @InterceptPosting('after')
	 */
	public function add_point_on_post($action, $args) {
//		var_dump($action,$args);
//		exit;
	}


	/** @Install */
	public static function install() {
		
		$sql = "CREATE TABLE IF NOT EXISTS ".self::$table_name."(
			user INT NOT NULL,
			point INT DEFAULT 0,
			PRIMARY KEY(user))";
		
		return self::$datasource->query($sql);
	}
}

?>
